{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-18T09:25:44.823115Z",
     "start_time": "2025-04-18T09:25:43.937056Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.21134911 0.26648055 0.34872551 4.11362892 4.07250645]\n",
      " [4.63455394 5.51921135 4.65952229 2.83783699 3.26768152]\n",
      " [2.54800092 3.12708608 2.63024587 1.29437888 1.54279898]\n",
      " [1.72584836 0.70743171 0.73906567 4.9211004  4.90528342]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 原始评分矩阵\n",
    "food = np.mat([[2,0,0,4,4],\n",
    "               [5,5,5,3,3],\n",
    "               [2,4,2,1,2],\n",
    "               [1,1,1,5,5]])\n",
    "\n",
    "# SVD分解\n",
    "U, sigma, Vt = np.linalg.svd(food)\n",
    "\n",
    "# 降维操作（取前2个特征）\n",
    "k = 2\n",
    "simple_food = U[:,:k] @ np.diag(sigma[:k]) @ Vt[:k,:]\n",
    "print(simple_food)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "75cabfbe58d47905",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1.21134911, 0.26648055, 0.34872551, 4.11362892, 4.07250645],\n",
       "        [4.63455394, 5.51921135, 4.65952229, 2.83783699, 3.26768152],\n",
       "        [2.54800092, 3.12708608, 2.63024587, 1.29437888, 1.54279898],\n",
       "        [1.72584836, 0.70743171, 0.73906567, 4.9211004 , 4.90528342]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simple_food"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4d3c55c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
